#
# fusion.http.Profiles
#
# Copyright 2007 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

'''
Handle URLs of the form "/profiles/".
'''

import web

from fusion.http.util import (
    withdb, format_xml, read_form,
    authorized_referer, unauthorized)
from fusion.http.server import MAIN

class Profiles(object):

    @withdb
    def GET(self, db):
        profiles = [c for c in db.list_collections()
                    if db.get(c, 'info/type') == 'tag:p2p-fusion,2007:profile']

        web.header('Content-Type', 'application/xhtml+xml; charset=UTF-8')

        print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
        print '    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
        print '<html xmlns="http://www.w3.org/1999/xhtml">'
        print '<head><title>Profiles</title></head>'
        print '<body>'

        print '<em>' + format_xml('a', 'admin', href='/') + '</em>'
        print '/'
        print '<em>' + format_xml('a', 'profiles', href='/profiles/') + '</em>'

        print '<p>'
        if not profiles:
            print '<em>No profiles</em>'
        else:
            print '<form action="/profiles/" method="post">'
            for c in profiles:
                title = db.get_text(c, 'info/title') or c
                url = MAIN + '/profile/' + c
                print format_xml('input', type='checkbox', name='checked', value=c)
                print format_xml('a', title, href=url)
                print '<br/>'
            print '<p>'
            print '<input type="submit" name="delete" value="Delete Checked" />'
            print '<input type="reset" />'
            print '</p>'
            print '</form>'
        print '</p>'

        print '</body>'
        print '</html>'

    @withdb
    def POST(self, db):
        if not authorized_referer():
            return unauthorized()

        form = read_form()
        if 'delete' in form:
            checked = form.getlist('checked')
            for c in checked:
                db.remove_collection(c)

            if db.get_local_info('default_profile') in checked:
                db.set_local_info('default_profile', None)

        return web.seeother('/profiles/')
